/**
 * @Title: OfferTemplate.java 
 * @Description: TODO
 * @author <a href="mailto:psammead@126.com">Psammead</a>
 * @date 2011-7-16 上午10:30:36 
 * @version 1.0
 * Copyright 2011 by Psammead
 */
package com.psammead.dms.offer.entity;

import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import com.psammead.dms.common.core.bo.AbstractBaseBO;
import com.psammead.dms.system.sercurity.entity.SysUser;

/**
 * @ClassName: OfferTemplate
 * @Description: TODO
 * @author <a href="mailto:psammead@126.com">psammead</a>
 * @version 1.0
 * @date 2011-7-16
 */
@Entity(name="OfferTemplate")
@Table(name="tb_of_template")
public class OfferTemplate extends AbstractBaseBO implements Serializable {

	/**
	 * @Fields serialVersionUID : TODO
	 */
	private static final long serialVersionUID = 3356190861907240019L;
	private long id;
	private String title;
	private String status;
	private String comment;
	private SysUser creator;
	private SysUser updater;
	private Date createTime;
	private Date updateTime;
	
	private Set<MaterialType> types=new HashSet<MaterialType>();
	private Set<MaterialDefine> defines=new HashSet<MaterialDefine>();
	
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name="template_id")
	@Override
	public long getId() {
		// TODO Auto-generated method stub
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}
	@Column(name="title")
	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	@Column(name="status")
	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	@Column(name="comment",length=1024)
	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	@ManyToOne(fetch=FetchType.LAZY, cascade={CascadeType.MERGE,
			CascadeType.REFRESH},optional=true)
	@JoinColumn(name="creator")
	public SysUser getCreator() {
		return creator;
	}

	public void setCreator(SysUser creator) {
		this.creator = creator;
	}
	
	@ManyToOne(fetch=FetchType.LAZY, cascade={CascadeType.MERGE,
			CascadeType.REFRESH},optional=true)
	@JoinColumn(name="updater")
	public SysUser getUpdater() {
		return updater;
	}

	public void setUpdater(SysUser updater) {
		this.updater = updater;
	}

	@Column(name="create_time")
	@Temporal(value=TemporalType.DATE)
	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	@Column(name="update_time")
	@Temporal(value=TemporalType.DATE)
	public Date getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(Date updateTime) {
		this.updateTime = updateTime;
	}

	@ManyToMany(cascade = CascadeType.REMOVE)
	@JoinTable(name = "tr_of_template_type", 
			joinColumns = {@JoinColumn(name = "template_id",nullable = false)}, 
			inverseJoinColumns = @JoinColumn(name = "type_id",nullable = false))
	public Set<MaterialType> getTypes() {
		return types;
	}

	public void setTypes(Set<MaterialType> types) {
		this.types = types;
	}

	public void addType(MaterialType type){
		if(!this.types.contains(type)){
			this.types.add(type);
		}
	}
	
	public void removeType(MaterialType type){
		this.types.remove(type);
	}

	@OneToMany(mappedBy="template",cascade=CascadeType.ALL)
	public Set<MaterialDefine> getDefines() {
		return defines;
	}

	public void setDefines(Set<MaterialDefine> defines) {
		this.defines = defines;
	}
	
	public void addDefine(MaterialDefine define){
		if(!this.defines.contains(define)){
			this.defines.add(define);
			define.setTemplate(this);
		}
	}
	
	public void removedDefine(MaterialDefine define){
		define.setTemplate(null);
		this.defines.remove(define);
	}
}
